package com.emily.test.controller;

import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.OPartner;
import com.emily.test.service.RedisService;
import com.emily.test.utils.HeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zc
 * @date ：Created in 2019/4/9 17:01
 * @description：
 */
@Slf4j
@RestController
@RequestMapping(value = "menu")
@Api(value = "获取官网后台菜单", description = "官网后台菜单接口", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class MenuController {
    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "获取合作伙伴列表", notes = "根据公司code获取合作伙伴列表")
    @PostMapping(value = "getMenuInfo")
    public ResponseResult getMenuInfo(HttpServletRequest request) {
        Map<String, Object> userMap = HeaderUtil.getUserinfo(request, redisService);
        List<Map<String, Object>> resourceList = (List<Map<String, Object>>) userMap.get("resourceList");
        try {
            List<Map<String, Object>> menuList = new ArrayList<>();
            for (Map<String, Object> map : resourceList) {
                if ("公司官网后台管理".equals(map.get("resName"))) {
                    List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("menuList");
                    for(Map<String, Object> map1 :  list){
                        Map<String, Object> menuMap = new HashMap<>();
                        menuMap.put("menuName",map1.get("menuName"));
                        menuMap.put("pageurl",map1.get("pageurl"));
                        menuList.add(menuMap);
                    }
                }
            }
            log.info("获取菜单列表={}", menuList);
            return ResponseResult.success(menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
}
